package org.deri.socializer.activities.config;


import org.deri.socializer.SocializerApplication;
import org.deri.socializer.activities.start.StartActivity;
import org.deri.vor.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

public class ConfigActivity extends Activity {
	
	SocializerApplication app;
	SharedPreferences preferences;
	
	boolean created = false;
	
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.configview);

        this.app = ((SocializerApplication)getApplicationContext());
        this.preferences = getSharedPreferences("myCustomSharedPrefs", Activity.MODE_PRIVATE);
    	
    	String xmppHost 	= preferences.getString("xmpp_host", SocializerApplication.DEFAULT_XMPP_HOST);
    	String xmppResource = preferences.getString("xmpp_resource", SocializerApplication.DEFAULT_RESOURCE);
    	String xmppUsername = preferences.getString("xmpp_username", "homer");
    	//String xmppPassword = preferences.getString("xmpp_password", "pwd");
        
    	EditText editTextHost = (EditText) findViewById(R.id.editTextXmppHost);
    	EditText editTextResource = (EditText) findViewById(R.id.editTextXmppResource);
    	EditText editTextUsername = (EditText) findViewById(R.id.editTextXmppUsername);
    	//EditText editTextPassword = (EditText) findViewById(R.id.editTextXmppHost);
        
    	editTextHost.setText(xmppHost);
    	editTextResource.setText(xmppResource);
    	editTextUsername.setText(xmppUsername);
    	
    	
    	final Button cancelButton = (Button) findViewById(R.id.buttonConfigCancel);
    	cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onCancelClick();
			}
        });

    	final Button okButton = (Button) findViewById(R.id.buttonConfigOK);
    	okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onOkClick();
			}
        });

    	
    	share_button();
    	
    	this.created = true;
    }

    private void onCancelClick() {
    	this.switchToStartActivity();    	
    }
    
    private void switchToStartActivity() {
    
    }
	
    private void onOkClick() {
    	if (this.savePreferences()) {
    		this.app.hasXmppConnection = true;
    		this.switchToStartActivity();
    	}
    	else {
    		this.showAlertDialog();
    	}
    }
    
    private boolean savePreferences() {
    	this.app.pauseApplication();
    	
    	EditText editTextHost = (EditText) findViewById(R.id.editTextXmppHost);
    	EditText editTextResource = (EditText) findViewById(R.id.editTextXmppResource);
    	EditText editTextUsername = (EditText) findViewById(R.id.editTextXmppUsername);
    	EditText editTextPassword = (EditText) findViewById(R.id.editTextXmppPassword);
    	CheckBox checkBoxDoRegister = (CheckBox) findViewById(R.id.checkBoxDoRegister);
    	
    	SharedPreferences.Editor editor = preferences.edit();
        editor.putString("xmpp_host", editTextHost.getText().toString().trim());
        editor.putString("xmpp_resource", editTextResource.getText().toString().trim());
        editor.putString("xmpp_username", editTextUsername.getText().toString().trim());
        editor.putString("xmpp_password", editTextPassword.getText().toString().trim());
        editor.commit();
        
        String host = editTextHost.getText().toString().trim();
        String resource = editTextResource.getText().toString().trim();
        String username = editTextUsername.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();
        boolean doRegister = checkBoxDoRegister.isChecked();
        
        boolean success = this.app.xmppConnect(host, resource, username, password, doRegister);
        
        if (success) {
        	if (doRegister)
        		return this.app.xmppConnect(host, resource, username, password, false);
        	else {
        		return true;
        	}
        } else {
        	return false;
        }
    }
    
    public void showAlertDialog() {
    	final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    	alertDialog.setTitle("XMPP connection failed!");
    	alertDialog.setMessage("Please check your settings");
    	alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
    	   public void onClick(DialogInterface dialog, int which) {
    	      // here you can add functions
    		  alertDialog.hide();
    	   }
    	});
    	alertDialog.show();
    }
    
    @Override
	protected void onPause() {
	    super.onPause();
	    this.created = false;
	    SocializerApplication.runningActivities--;
	}
    
	@Override
	protected void onResume() {
		super.onResume();
		if (!created) {
	    	Intent intent = getIntent();
	    	finish();
	    	startActivity(intent);
	    }
		SocializerApplication.runningActivities++;
	}
	
	@Override
	protected void onDestroy() {
	    super.onDestroy();
	    this.app.checkActivityState();
	}
	
	private void share_button(){
		Button button = (Button) findViewById(R.id.btnBack);
		button.setOnClickListener(ms_layout);

		// About Button OnClick
		button = (Button) findViewById(R.id.btnAbout);
		button.setOnClickListener(ms_layout);

		// Home Button OnClick
		button = (Button) findViewById(R.id.btnHome);
		button.setOnClickListener(ms_layout);
		
		ImageView im  = (ImageView) findViewById(R.id.vor_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);

		im = (ImageView) findViewById(R.id.nuig_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);

		im = (ImageView) findViewById(R.id.deri_icon);
		im.setClickable(true);
		im.setOnClickListener(ms_layout);
	}
    
    
    private OnClickListener ms_layout	=	new OnClickListener(){

		@Override
		public void onClick(View v) {
			switch (v.getId()){
			case R.id.btnAbout  : { jump(72); break;}
			case R.id.btnHome 	: { jump(2); break;}
			case R.id.btnBack  	: { back(); break;}
			case R.id.vor_icon  : { jump(3); break;}
			case R.id.deri_icon : { jump(19); break;}
			case R.id.nuig_icon : { jump(40); break;}
			}
		}
    };
    
    
    private void jump(int page){
    	Intent i = new Intent();
    	i.putExtra("cl",page);
    	i.setClassName("org.deri.vor", "org.deri.vor.VovolOceanRaceActivity");
		startActivity(i);
		finish();
    }
   
    public void back() {
    	Intent intent = new Intent(getApplicationContext(), StartActivity.class);
    	startActivity(intent);
    	finish();
    
    }
		
		
		
	    
}
